package com.je21121.controller;

import com.google.gson.Gson;
import com.je21121.model.MyPage;
import com.je21121.model.UserInfo;
import com.je21121.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.xml.ws.RequestWrapper;
import java.io.IOException;
import java.util.Arrays;
import java.util.Hashtable;
import java.util.List;

// 用户servlet  用户的增删改查方法
@WebServlet("/userCon")
public class UserController extends HttpServlet {
    UserService userService = new UserService();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 判断 参数  调用对应的方法
        String method = request.getParameter("method");
        System.out.println("参数名："+method);
        if("updateUser".equals(method)){
           // login(request,response);//修改用户
        }else if("deleteUser".equals(method)){
            deleteUser(request,response);// 删除用户
        }else if("userlist".equals(method)){
            // 会员列表
            getUserlist(request,response);
        }else if("adduser".equals(method)){
            // 增加用户
            adduser(request,response);
        }else if("getuser".equals(method)){
            //  获取单个用户
            getuser(request,response);
        } else if("getupdateuser".equals(method)){
            //  获取 修改用户数据
            getupdateuser(request,response);
        }else if("updateuser".equals(method)){
            //   修改用户数据
            updateuser(request,response);
        }
    }

    //修改用户
    private void updateuser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer uid = Integer.valueOf(request.getParameter("userid"));
        System.out.println(uid);
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Integer type = Integer.valueOf(request.getParameter("type"));
        UserInfo userInfo = new UserInfo(uid,username,password,type);
        System.out.println(userInfo);
        //  调用jdbc 修改用户
        boolean flag = userService.updateuser(userInfo);
        if(flag){
            response.getWriter().print("修改用户成功");
        }else{
            response.getWriter().print("修改用户失败");
        }
    }

    private void getupdateuser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        //   通过参数  判断 返回的页面
        String returnpage = request.getParameter("returnpage");
        System.out.println(uid);
        UserInfo userInfo = new UserInfo();
        userInfo.setId(uid);
        UserInfo userInfo1 = userService.getuser(userInfo);
        request.setAttribute("u",userInfo1);
        if("1".equals(returnpage)){//  返回 编辑页面
            request.getRequestDispatcher("member-edit.jsp").forward(request,response);
        }else{//  返回 密码修改页面
            request.getRequestDispatcher("member-password.jsp").forward(request,response);
        }
    }

    private void getuser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        Integer uid = Integer.valueOf(request.getParameter("uid"));
        //  通过用户id 获取用户数据
        System.out.println(uid);
        UserInfo userInfo = new UserInfo();
        //  对象参数  传递给dao
        userInfo.setId(uid);
        //  返回用户数据  用户对象属性 比较多的时候
        UserInfo userInfo1 = userService.getuser(userInfo);
        request.setAttribute("u",userInfo1);
        //  把用户数据  传到页面中
        request.getRequestDispatcher("member-show.jsp").forward(request,response);
    }

    private void getUserlist(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //  获取前端页码    
        String curpage = request.getParameter("curpage");
        System.out.println(curpage);
        // 判断 是否为空
        if(curpage == null || "".equals(curpage)){
            curpage = "1"; //表示查询第一页
        }
        //  创建一个page对象
        MyPage myPage = new MyPage();
        String username = request.getParameter("username");
        //   模糊查询   把输入值 传回去
        request.setAttribute("username",username);
        System.out.println(username);
        //  获取最大记录数 整个用户表的
        int count = userService.getUserCount(username);
        System.out.println("最大记录数"+count);
        myPage.setMaxcount(count);
        //myPage.setMaxpage(3);
        myPage.setCurpage(Integer.parseInt(curpage));
        UserInfo userInfo = new UserInfo();
        //  把page 参数 放到user对象里面
        userInfo.setPage(myPage);
        userInfo.setUsername(username);
        System.out.println(myPage);
        //  返回用户集合数据
        List<UserInfo> list = userService.getUserlist(userInfo);
        request.setAttribute("list",list);
        //  把用户数据  传到页面中
        //  把page传回前端
        request.setAttribute("myPage",myPage);//  map.put("myPage",myPage)
        request.getRequestDispatcher("member-list.jsp").forward(request,response);
    }

    //增加用户
    //maping("/add")
    private void adduser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        Integer type = Integer.valueOf(request.getParameter("type"));
        UserInfo userInfo = new UserInfo(username,password,type);
        System.out.println(userInfo);
        //  调用jdbc 添加用户
        boolean flag = userService.addUser(userInfo);
        if(flag){
            response.getWriter().print("添加用户成功");
        }else{
            response.getWriter().print("添加用户失败");
        }
    }
    // 删除用户
    private void deleteUser(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取所有的用户id
        String ids =  request.getParameter("ids");
        System.out.println(ids);
        Gson gson = new Gson();
        //   通过gson  把前端传输的 json字符串 转换成 java对象
        Integer[] uids = gson.fromJson(ids, Integer[].class);
        System.out.println(uids);
        //  调用服务  批量删除用户
        boolean flag = userService.deleteUser(uids);
        if(flag){
            response.getWriter().print("删除用户成功");
        }else{
            response.getWriter().print("删除用户失败");
        }
    }

}
